//
//  PersonalSetTableViewCell.m
//  Products_B
//
//  Created by lanou on 16/6/4.
//  Copyright © 2016年 朋学友. All rights reserved.
//

#import "PersonalSetTableViewCell.h"
@interface PersonalSetTableViewCell()<UIImagePickerControllerDelegate, UINavigationControllerDelegate> {
    UILabel *_leftLabel;
    UILabel *_label1;
    UILabel *_label2;
    UIImageView *_header_imageView;
}


@end
@implementation PersonalSetTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        _leftLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, (self.frame.size.height - 20) / 2, kWidth / 2, 20)];
        _leftLabel.font = [UIFont systemFontOfSize:15];
        _leftLabel.alpha = 0.8;
        [self.contentView addSubview:_leftLabel];
    }
    return self;
}

- (void)setIndexPath:(NSIndexPath *)indexPath {
    _indexPath = indexPath;
    if (indexPath.section == 0) {
        _leftLabel.text = @"头像";
        
        _header_imageView = [[UIImageView alloc] initWithFrame:CGRectMake(kWidth - 70, 10, 40, 40)];
        _header_imageView.layer.cornerRadius = 20;
        _header_imageView.layer.masksToBounds = YES;
        _header_imageView.userInteractionEnabled = YES;
        _header_imageView.image = [UIImage imageNamed:@"MyChushi.png"];
        [self.contentView addSubview:_header_imageView];
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
        [_header_imageView addGestureRecognizer:tap];
    }
    if (indexPath.section == 1) {
        if (indexPath.row == 0) {
            _leftLabel.text = @"昵称";
            _nickLabel = [[UILabel alloc] initWithFrame:CGRectMake(kWidth - 90, (self.frame.size.height - 20) / 2, 60, 20)];
            _nickLabel.text = @"食神";
            _nickLabel.alpha = 0.5;
            _nickLabel.font = [UIFont systemFontOfSize:14];
            [self.contentView addSubview:_nickLabel];
            
        } else if (indexPath.row == 1) {
            _leftLabel.text = @"ID";
            _idLabel = [[UILabel alloc] initWithFrame:CGRectMake(kWidth - 90, (self.frame.size.height - 20) / 2, 60, 20)];;
            _idLabel.text = @"1513438";
            _idLabel.alpha = 0.5;
            _idLabel.font = [UIFont systemFontOfSize:14];
            [self.contentView addSubview:_idLabel];
            
        } else if (indexPath.row == 2) {
            self.selectionStyle = UITableViewCellSelectionStyleNone;
            _leftLabel.text = @"性别";
            
            UIButton *manButton = [UIButton buttonWithType:UIButtonTypeCustom];
            manButton.frame = CGRectMake(kWidth - 120, 17, 10, 10);
            [manButton setImage:[UIImage imageNamed:@"MyYuanQuan.png"] forState:UIControlStateNormal];
            [manButton addTarget:self action:@selector(manButtonAction:) forControlEvents:UIControlEventTouchUpInside];
            [self.contentView addSubview:manButton];
            
            UILabel *manLabel = [[UILabel alloc] initWithFrame:CGRectMake(kWidth - 105, 11, 20, 20)];
            manLabel.text = @"男";
            manLabel.font = [UIFont systemFontOfSize:12];
            manLabel.alpha = 0.5;
            [self.contentView addSubview:manLabel];
            
            _label1 = [[UILabel alloc] initWithFrame:manButton.bounds];
            _label1.text = @"●";
            _label1.textAlignment = NSTextAlignmentCenter;
            _label1.font = [UIFont systemFontOfSize:12];
            _label1.textColor = [UIColor orangeColor];
            // 之所以按woman取非而不直接按man取值是为了程序第一次运行时，如果按man取值，此时本地存储的值默认为NO，下面的woman同理。如果为NO，就意味着程序第一次运行时，男女同时会显示
            _label1.hidden = ![[NSUserDefaults standardUserDefaults] boolForKey:@"woman"];
            [manButton addSubview:_label1];
            
            UIButton *womanButton = [UIButton buttonWithType:UIButtonTypeCustom];
            womanButton.frame = CGRectMake(kWidth - 60, 17, 10, 10);
            [womanButton setImage:[UIImage imageNamed:@"MyYuanQuan.png"] forState:UIControlStateNormal];
            [womanButton addTarget:self action: @selector(womanButtonAction:) forControlEvents:UIControlEventTouchUpInside];
            [self.contentView addSubview:womanButton];
            
            UILabel *womanLabel = [[UILabel alloc] initWithFrame:CGRectMake(kWidth - 45, 11, 20, 20)];
            womanLabel.text = @"女";
            womanLabel.font = [UIFont systemFontOfSize:12];
            womanLabel.alpha = 0.5;
            [self.contentView addSubview:womanLabel];
            
            _label2 = [[UILabel alloc] initWithFrame:womanButton.bounds];
            _label2.text = @"●";
            _label2.textAlignment = NSTextAlignmentCenter;
            _label2.font = [UIFont systemFontOfSize:12];
            _label2.textColor = [UIColor orangeColor];
            _label2.hidden = ![[NSUserDefaults standardUserDefaults] boolForKey:@"man"];
            [womanButton addSubview:_label2];
            NSLog(@" .... %@",NSHomeDirectory());
        } else {
            _leftLabel.text = @"生日";
        }
    }
    if (indexPath.section == 2) {
        _leftLabel.text = @"个人介绍";
    }
    if (indexPath.section == 3) {
        _leftLabel.text = @"家乡";
        
        _hometownLabel = [[UILabel alloc] initWithFrame:CGRectMake(kWidth - 90, (self.frame.size.height - 20) / 2, 60, 20)];;
        _hometownLabel.text = @"上海市";
        _hometownLabel.alpha = 0.5;
        _hometownLabel.font = [UIFont systemFontOfSize:14];
        [self.contentView addSubview:_hometownLabel];
        [self.contentView addSubview:_hometownLabel];
    }
}

- (void)tapAction:(UITapGestureRecognizer *)tap {
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    // 相机按钮
    UIAlertAction *cameraAction = [UIAlertAction actionWithTitle:@"相机" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"相机"); // 花括号内要执行的是对应按钮要做的事情
    }];
    [alert addAction:cameraAction];
    
    // 相册按钮
    UIAlertAction *pictureAction = [UIAlertAction actionWithTitle:@"从相册中选取" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        // 判断有没有相册
        if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
            // 创建相册对象
            UIImagePickerController *picker = [[UIImagePickerController alloc] init];
            picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
            // 是否可以编辑
            picker.allowsEditing = YES;
            // 设置代理 代理需要遵守两个协议
            picker.delegate = self;
            [[self viewController] presentViewController:picker animated:YES completion:nil];
        }
    }];
    [alert addAction:pictureAction];
    
    // 取消按钮  取消按钮的style必须是UIAlertActionStyleCancel
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"取消");
    }];
    [alert addAction:cancelAction];
    // 让alert以动画形式出现
    [[self viewController] presentViewController:alert animated:YES completion:nil];
    
    NSLog(@" +++++ %@",[self viewController]);
}

#pragma mark - 获取当前view所在的视图控制器
- (UIViewController *)viewController {
    for (UIView* next = [self superview]; next; next = next.superview) {
        UIResponder* nextResponder = [next nextResponder];
        if ([nextResponder isKindOfClass:[UIViewController class]]) {
            return (UIViewController*)nextResponder;
        }
    }
    return nil;
}

#pragma mark ---- 选择完照片 或者 拍摄完成后 调用的方法---
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info {
    NSLog(@"%@",info);
    _header_imageView.image = [info objectForKey:UIImagePickerControllerEditedImage];
    [[self viewController] dismissViewControllerAnimated:YES completion:nil];
}

- (void)manButtonAction:(UIButton *)manButton {
    _label1.hidden = NO;
    _label2.hidden = YES;
    [self save];
}

- (void)womanButtonAction:(UIButton *)womanButton {
    _label2.hidden = NO;
    _label1.hidden = YES;
    [self save];
}

- (void)save {
    [[NSUserDefaults standardUserDefaults] setBool:_label1.hidden forKey:@"man"];
    [[NSUserDefaults standardUserDefaults] setBool:_label2.hidden forKey:@"woman"];
    [[NSUserDefaults standardUserDefaults] synchronize];
}



- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
